package com.coocaa.x.app.hall.main.ui;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.TextView;

import com.coocaa.x.app.hall.HallBaseActivity;
import com.coocaa.x.app.hall.main.control.PageController;
import com.coocaa.x.app.hall.main.ui.category.CategoryBar;
import com.coocaa.x.app.hall.view.widget.TipToast;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.app.CoocaaApplication.NetChangeReceiver.NetChangeReceiverListener;
import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.framework.utils.AsyncTask;
import com.coocaa.x.framework.utils.LiteURI;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litecontent.LiteContentServiceConnecter;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_hall.RP_Content;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_hall.RP_TagList;
import com.coocaa.x.uipackage.generallayout.view.BaseCategoryBar.BaseCategoryData;
import com.coocaa.x.app.hall.parser.layoutparser.LayoutParser;
import com.coocaa.x.uipackage.BaseView;
import com.coocaa.x.uipackage.generallayout.GeneralPageLayout;
import com.coocaa.x.app.hall.R;
import com.coocaa.x.uipackage.generallayout.view.BasePageView;
import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageLayout extends BaseView implements NetChangeReceiverListener {

    protected static final String tag = "PageLayout";
    protected static final int GET_CATEGORY = 1001;
    protected static final int GET_FIRSTROOTJSON = 1002;
    protected static final int GET_ROOTJSON = 1003;
    protected static final int REQUEST_FOCUS = 1004;
    protected static final int NETWORK_CONNECT = 1005;
    protected static final int SHOW_LOADING = 1006;
    protected static final int DISMISS_LOADING = 1007;
    protected static final int SHOW_TIPS = 1008;
    protected static final int TIMEOUT_EXIT = 1009;
    protected static final int RETRY_GETDATA = 1010;
    protected static final String THREADID = "threadID";
    private Context mContext;
    private GeneralPageLayout mGeneralPageLayout;
    private PageController mPageController = null;
    private TitleType isCategory = TitleType.HOME_PAGE;
    private Map<String, List<String>> pageJsonMap = null;
    private Map<String, Integer> pageTypeMap = null;

    private List<BaseCategoryData> listCategoryData = null;
    private List<LayoutParser> childViewList = null;
    private SlideFocusView slide = null;
    private String mListTitleName;
    private String mListId;
    private boolean isRetry = true;
    private boolean isConnect = true;
    private boolean isTimeoutExit = false;
    private String hallName = null;
    private String firstTagName = null;
    private boolean pageItemRequestedFocus = false;
    private String mTagPic = null;
    private CategoryBar mCategoryBar = null;
    private TextView versionText;
    private int newThreadId = 0;

    @Override
    public void onNetStateChanged(boolean bConnected) {
        isConnect = bConnected;
        LogUtils.i(tag, "bConnected: " + bConnected + ", getChildCount: ");
        if (bConnected == true && (pageJsonMap == null || pageJsonMap.size() == 0)) {
            mHandler.sendEmptyMessage(NETWORK_CONNECT);
        }
    }

    public enum TitleType {
        HOME_PAGE,
        LIST_PAGE
    }

    public PageLayout(Context context, TitleType type) {
        super(context);
        // TODO Auto-generated constructor stub
        mContext = context;
        isCategory = type;

        mPageController = new PageController(mContext);
        slide = new SlideFocusView(mContext, R.drawable.tag_focus);

        versionText = new TextView(mContext);
        versionText.setAlpha(0.1f);
        versionText.setTextSize(CoocaaApplication.Dpi(18));
        String version = CoocaaApplication.getAPPVersion();
        versionText.setText("V" + version);
        CoocaaApplication.addNetChangedListener(this);
    }

    public void setListTitleId(String id) {
        mListId = id;
    }

    public void refreshLayout(final String name) {
        LogUtils.i(tag, "refreshLayout : " + name);

        if (!Android.isNetConnected(mContext)) {
            //网络异常提示
            TipToast.getInstance().showToast(mContext.getString(R.string.connect_Internet));
            LogUtils.i(tag, "refreshLayout net disconnect.");
        }

        if (hallName != null && hallName.equals(name) && pageJsonMap != null && pageJsonMap.size() > 0
                && listCategoryData != null && listCategoryData.size() == pageJsonMap.size()) {
            if (pageItemRequestedFocus) {
                Message message = new Message();
                message.what = REQUEST_FOCUS;
                message.arg1 = -1;
                mHandler.sendMessageDelayed(message, 100);
            }
            LogUtils.i(tag, "return refreshLayout");
            return;
        } else {
            hallName = name;
        }

        mTagPic = null;
        firstTagName = null;
        isTimeoutExit = false;
        pageItemRequestedFocus = false;
        isRetry = true;
        hideTips();

        if (mPageController != null)
            mPageController.onDestroy();

        if (slide != null)
            slide.hideFocusView();
        if (childViewList != null) {
            childViewList.clear();
        }

        if (isCategory == TitleType.HOME_PAGE) {
            if (mCategoryBar != null) {
                mCategoryBar.onDestory();
                mCategoryBar = null;
            }
        }

        if (mGeneralPageLayout != null) {
            mGeneralPageLayout.onDestory();
            this.removeView(mGeneralPageLayout);
            mGeneralPageLayout.removeAllViews();
            mGeneralPageLayout = null;
        }

        if (isHasParent())
            showLoading();
        else
            mHandler.sendEmptyMessageDelayed(SHOW_LOADING, 100);

        mHandler.removeMessages(TIMEOUT_EXIT);
        mHandler.sendEmptyMessageDelayed(TIMEOUT_EXIT, 30000);
        newThreadId++;
        getRecommendData();

        CoocaaApplication.post(new Runnable() {
            @Override
            public void run() {
                Map<String, String> map = new HashMap<String, String>();
                map.put("hall", name);
                String json = JSONObject.toJSONString(map);
                try {
                    XServiceManager.getLiteCCConnectorService().collectSpecialpAppStart(mContext.getPackageName(), name, json);
                    LogUtils.d("HALL", "collectSpecialpAppStart " + mContext.getPackageName() + " name:" + name + " json:" + json);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void createView() {
        if (childViewList != null) {
            childViewList.clear();
        }

        if (mGeneralPageLayout != null) {
            mGeneralPageLayout.onDestory();
            this.removeView(mGeneralPageLayout);
            mGeneralPageLayout.removeAllViews();
            mGeneralPageLayout = null;
        }

        mGeneralPageLayout = new GeneralPageLayout(mContext);
        mGeneralPageLayout.getPagerLayout().addView(slide);
        mPageController.mSlideFocusView = slide;
        mPageController.initController(hallName);

        mPageController.setBasePageLayout(mGeneralPageLayout);
        mGeneralPageLayout.stePageFocusListener(mPageController);

        if (isCategory == TitleType.HOME_PAGE) {
            if (mCategoryBar != null) {
                mCategoryBar.onDestory();
                mCategoryBar = null;
            }
            mCategoryBar = new CategoryBar(mContext);
            mGeneralPageLayout.addCategoryBar(mCategoryBar);
        } else {
            mGeneralPageLayout.addTitleView();
        }

        LayoutParams versionParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        versionParams.leftMargin = CoocaaApplication.Div(148);
        versionParams.topMargin = CoocaaApplication.Div(1025);
        mGeneralPageLayout.addView(versionText, versionParams);
        this.addView(mGeneralPageLayout);
    }

    public void onResume() {
        if (mPageController != null && mGeneralPageLayout != null && mGeneralPageLayout.mTotalPage > 0) {
            mPageController.onResume();
            if (mCategoryBar != null) {
                mCategoryBar.onResume();
            }
        }
    }

    private void getRecommendData() {
        /**
         * 增加线程启动标识，防止网络不好快速退出启动时，出现多个线程同时运行情况，
         * 获取到了多份后台数据。
         */
        final int threadId = newThreadId;
        AsyncTask.runOnThread(new Runnable() {
            @Override
            public void run() {

                LogUtils.i(tag, "threadId: " + threadId);
                String result;

                Map<String, List<String>> pageJsonMapTemp = new HashMap<String, List<String>>();
                Map<String, Integer> pageTypeMapTemp = new HashMap<String, Integer>();
                List<BaseCategoryData> listCategoryDataTemp = new ArrayList<BaseCategoryData>();

                //调用后台接口获取category数据
                if (isCategory == TitleType.HOME_PAGE) {

                    LiteURI uri = LiteURI.create("store://com.coocaa.hall/tagdata");

                    uri.addParams("name", hallName);
                    try {
                        LogUtils.i(tag, "start get tag data.");
                        result = LiteContentServiceConnecter.getService().call(uri.toString());
                        //如果不是最新的线程则退出
                        LogUtils.i(tag, "newThreadId = " + newThreadId + ", threadId: " + threadId);
                        if (newThreadId != threadId)
                            return;

                        RP_TagList mHallTabData = JSONObject.parseObject(result, RP_TagList.class);

                        if (isTimeoutExit) {
                            return;
                        }

                        if (mHallTabData != null && mHallTabData.error_code != -1) {
                            LogUtils.i(tag, "get tag data success. tag size = " + mHallTabData.resource.resource_items.size());
                            for (RP_TagList.Tag_Item tabInfo : mHallTabData.resource.resource_items) {

                                BaseCategoryData cdata = new BaseCategoryData(tabInfo.id, tabInfo.title);
                                listCategoryDataTemp.add(cdata);
                                LogUtils.i(tag, "tabInfo.tagpic: " + tabInfo.tagpic);
                                if (mTagPic == null && tabInfo.tagpic != null) {
                                    mTagPic = tabInfo.tagpic;
                                }
                            }
                        }
                    } catch (RemoteException e) {
                        LogUtils.i(tag, "error: " + e);
                        mHandler.sendEmptyMessage(RETRY_GETDATA);
                        return;
                    } catch (Exception e) {
                        mHandler.sendEmptyMessage(RETRY_GETDATA);
                        return;
                    }

                    if (listCategoryDataTemp.size() > 0) {
                        if (newThreadId != threadId)
                            return;
                        if (listCategoryData != null)
                            listCategoryData.clear();
                        else
                            listCategoryData = new ArrayList<BaseCategoryData>();
                        listCategoryData.addAll(listCategoryDataTemp);

                        Message message = new Message();
                        message.what = GET_CATEGORY;
                        Bundle bundle = new Bundle();
                        bundle.putInt(THREADID, threadId);
                        message.setData(bundle);
                        mHandler.sendMessage(message);
                    } else {
                        //提示异常
                        if (isHasParent()) {
                            mHandler.removeMessages(SHOW_TIPS);
                            mHandler.removeMessages(SHOW_LOADING);
                            mHandler.removeMessages(TIMEOUT_EXIT);
                            showTips("数据接收错误");
                            hideLoading();
                        } else {
                            mHandler.sendEmptyMessageDelayed(SHOW_TIPS, 500);
                            mHandler.sendEmptyMessageDelayed(DISMISS_LOADING, 500);
                        }
                        return;
                    }
                }

                if (isCategory == TitleType.HOME_PAGE) {

                    try {
                        for (BaseCategoryData cdata : listCategoryDataTemp) {
                            LiteURI uri = LiteURI.create("store://com.coocaa.hall/contentdata");
                            uri.addParams("id", cdata.id);
                            uri.addParams("index", "-1");
                            uri.addParams("count", "4");

                            result = LiteContentServiceConnecter.getService().call(uri.toString());
                            //如果不是最新的线程则退出
                            LogUtils.i(tag, "newThreadId = " + newThreadId + ", threadId: " + threadId);
                            if (newThreadId != threadId)
                                return;

                            RP_Content mHallPageData = JSONObject.parseObject(result, RP_Content.class);

                            if (mHallPageData != null && mHallPageData.error_code != -1) {

                                pageJsonMapTemp.put(cdata.name, mHallPageData.resource.resource_items);
                                pageTypeMapTemp.put(cdata.name, mHallPageData.head.type);
                                if (pageJsonMapTemp.size() == 1) {
                                    Message message = new Message();
                                    message.what = GET_FIRSTROOTJSON;
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("pageType", mHallPageData.head.type);
                                    bundle.putInt(THREADID, threadId);
                                    bundle.putString("name", cdata.name);
                                    bundle.putStringArrayList("pageJson", (ArrayList<String>) mHallPageData.resource.resource_items);
                                    message.setData(bundle);
                                    mHandler.sendMessage(message);
                                }
                            }
                        }
                        listCategoryDataTemp.clear();
                    } catch (RemoteException e) {
                        LogUtils.i(tag, "" + e);
                        mHandler.sendEmptyMessage(RETRY_GETDATA);
                        return;
                    } catch (Exception e) {
                        LogUtils.i(tag, "" + e);
                        mHandler.sendEmptyMessage(RETRY_GETDATA);
                        return;
                    }
                } else if (isCategory == TitleType.LIST_PAGE) {
                    try {
                        LiteURI uri = LiteURI.create("store://com.coocaa.hall/contentdata");
                        uri.addParams("id", mListId);
                        uri.addParams("index", "-1");
                        uri.addParams("count", "4");
                        result = LiteContentServiceConnecter.getService().call(uri.toString());
                        //如果不是最新的线程则退出
                        LogUtils.i(tag, "newThreadId = " + newThreadId + ", threadId: " + threadId);
                        if (newThreadId != threadId)
                            return;

                        RP_Content mHallPageData = JSONObject.parseObject(result, RP_Content.class);

                        if (mHallPageData != null && mHallPageData.error_code != -1) {
                            mListTitleName = mHallPageData.head.title;
                            mListId = mHallPageData.head.id;
                            pageJsonMapTemp.put(mListTitleName, mHallPageData.resource.resource_items);
                        }
                    } catch (RemoteException e) {
                        LogUtils.i(tag, "" + e);
                    } catch (Exception e) {
                        LogUtils.i(tag, "" + e);
                    }
                }

                if (pageJsonMapTemp.size() > 0) {
                    if (newThreadId != threadId)
                        return;
                    if (pageJsonMap == null)
                        pageJsonMap = new HashMap<String, List<String>>();
                    else
                        pageJsonMap.clear();

                    if (pageTypeMap == null)
                        pageTypeMap = new HashMap<String, Integer>();
                    else
                        pageTypeMap.clear();

                    pageJsonMap.putAll(pageJsonMapTemp);
                    pageTypeMap.putAll(pageTypeMapTemp);

                    Message message = new Message();
                    message.what = GET_ROOTJSON;
                    Bundle bundle = new Bundle();
                    bundle.putInt(THREADID, threadId);
                    message.setData(bundle);
                    mHandler.sendMessage(message);
                } else {
                    //提示异常
                    if (isHasParent()) {
                        mHandler.removeMessages(SHOW_TIPS);
                        mHandler.removeMessages(SHOW_LOADING);
                        showTips("数据接收错误");
                        hideLoading();
                    } else {
                        mHandler.sendEmptyMessageDelayed(SHOW_TIPS, 500);
                        mHandler.sendEmptyMessageDelayed(DISMISS_LOADING, 500);
                    }
                    return;
                }
            }
        });
    }

    private void refreshCategory() {
        LogUtils.i(tag, "listCategoryData size =" + listCategoryData.size());
        createView();

        if (mCategoryBar != null) {
            LogUtils.i(tag, "mTagPic: " + mTagPic);
            mCategoryBar.refreshLayout(mTagPic);
        }

        if (childViewList != null)
            childViewList.clear();
        else
            childViewList = new ArrayList<LayoutParser>();

        List<BaseCategoryData> mListCategoryData = new ArrayList<BaseCategoryData>();
        synchronized (listCategoryData) {
            mListCategoryData.addAll(listCategoryData);
        }

        for (BaseCategoryData baseData : mListCategoryData) {
            LogUtils.i(tag, "name =" + baseData.name);
            if (baseData != null && baseData.id != null && baseData.name != null) {
                LayoutParser parser = new LayoutParser(mContext);
                parser.id = baseData.id;
                parser.name = baseData.name;
                parser.setItemFocusChangeListener(mPageController);
                parser.setItemKeyEventListener(mPageController);
                parser.setOnScrollEndListener(mPageController);
                parser.setOnItemClickListener(mPageController);
                parser.setIItemViewLoadoverListener(mPageController);
                parser.setmFocusChangedEvent(slide.focusChangedEvent);
                childViewList.add(parser);
                mGeneralPageLayout.addPageChildView(parser);
            }
        }

        mListCategoryData.clear();
        mGeneralPageLayout.setCategoryFocus();
        mGeneralPageLayout.setCategoryKey(false);
    }

    Handler mHandler = new Handler() {

        @Override
        public void dispatchMessage(Message msg) {
            // TODO Auto-generated method stub
            Bundle bundle;
            super.dispatchMessage(msg);
            switch (msg.what) {
                case GET_CATEGORY:
                    mHandler.removeMessages(TIMEOUT_EXIT);
                    bundle = msg.getData();
                    if (bundle == null || bundle.getInt(THREADID, 0) != newThreadId) {
                        LogUtils.i(tag, "GET_CATEGORY thread not equal");
                        break;
                    }
                    refreshCategory();
                    break;
                case GET_FIRSTROOTJSON:
                    LayoutParser firstParser = null;
                    bundle = msg.getData();
                    if (bundle == null || childViewList == null
                            || bundle.getInt(THREADID, 0) != newThreadId) {
                        LogUtils.i(tag, " GET_FIRSTROOTJSON thread not equal");
                        break;
                    }
                    int index = 0;
                    int pageType = bundle.getInt("pageType", 0);
                    String name = bundle.getString("name", " ");
                    List<String> jsonList = bundle.getStringArrayList("pageJson");

                    for (BasePageView baseView : childViewList) {
                        if (name.equals(baseView.name)) {
                            firstParser = (LayoutParser) baseView;
                            if (firstParser == null)
                                continue;
                            if (pageType == 2) {
                                firstParser.initCustomLayout(LayoutParser.LayoutType.LIST_PAGE);
                            } else {
                                firstParser.initCustomLayout(LayoutParser.LayoutType.HOME_PAGE);
                            }

                            firstParser.parse(jsonList);
                            firstTagName = name;
                            break;
                        }
                        index++;
                    }

                    mGeneralPageLayout.setClickChangePager(false);
                    mGeneralPageLayout.displayedChild(index);
                    Message message = new Message();
                    message.what = REQUEST_FOCUS;
                    message.arg1 = bundle.getInt(THREADID, 0);
                    mHandler.sendMessage(message);
                    pageItemRequestedFocus = true;
                    mHandler.sendEmptyMessage(DISMISS_LOADING);
                    break;
                case GET_ROOTJSON:
                    bundle = msg.getData();

                    if (bundle == null || bundle.getInt(THREADID, 0) != newThreadId) {
                        break;
                    }

                    if (isCategory == TitleType.HOME_PAGE && childViewList == null) {
                        LogUtils.i(tag, "childViewList == null");
                        break;
                    }

                    if (isCategory == TitleType.HOME_PAGE) {
                        LayoutParser parser = null;
                        for (BasePageView baseView : childViewList) {
                            parser = (LayoutParser) baseView;
                            if (baseView == null || parser == null || baseView.name.equals(firstTagName))
                                continue;

                            if (pageTypeMap.containsKey(baseView.name) && pageTypeMap.get(baseView.name) == 2) {
                                parser.initCustomLayout(LayoutParser.LayoutType.LIST_PAGE);
                            } else {
                                parser.initCustomLayout(LayoutParser.LayoutType.HOME_PAGE);
                            }

                            if (pageJsonMap.containsKey(baseView.name)) {
                                parser.parse(pageJsonMap.get(baseView.name));
                            } else {
                                TextView textView = new TextView(mContext);
                                textView.setTextSize(CoocaaApplication.Dpi(40));
                                textView.setText("数据接收错误");
                                LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                                        LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.CENTER);
                                parser.addView(textView, params);
                            }
                        }

                        if (firstTagName == null) {
                            //请求焦点
                            LogUtils.i(tag, "REQUEST_FOCUS");
                            mGeneralPageLayout.setClickChangePager(false);
                            mGeneralPageLayout.displayedChild(0);
                            Message message1 = new Message();
                            message1.what = REQUEST_FOCUS;
                            message1.arg1 = bundle.getInt(THREADID, 0);
                            mHandler.sendMessage(message1);
                            pageItemRequestedFocus = true;
                            mHandler.sendEmptyMessage(DISMISS_LOADING);
                        }
                    } else if (isCategory == TitleType.LIST_PAGE) {
                        LogUtils.i(tag, "isCategory == TitleType.LIST_PAGE");
                        mHandler.removeMessages(TIMEOUT_EXIT);
                        createView();
                        mGeneralPageLayout.setTitle(mListTitleName);

                        LayoutParser parser = new LayoutParser(mContext);
                        parser.id = mListId;
                        parser.name = mListTitleName;
                        parser.initCustomLayout(LayoutParser.LayoutType.LIST_PAGE);
                        parser.setItemFocusChangeListener(mPageController);
                        parser.setItemKeyEventListener(mPageController);
                        parser.setOnScrollEndListener(mPageController);
                        parser.setOnItemClickListener(mPageController);
                        parser.setIItemViewLoadoverListener(mPageController);
                        parser.setmFocusChangedEvent(slide.focusChangedEvent);
                        parser.parse(pageJsonMap.get(mListTitleName));

                        mGeneralPageLayout.addPageChildView(parser);
                        mGeneralPageLayout.setClickChangePager(false);
                        mGeneralPageLayout.displayedChild(0);
                        Message message1 = new Message();
                        message1.what = REQUEST_FOCUS;
                        message1.arg1 = bundle.getInt(THREADID, 0);
                        mHandler.sendMessageDelayed(message1, 1000);
                        pageItemRequestedFocus = true;
                        mHandler.sendEmptyMessage(DISMISS_LOADING);
                    }
                    break;
                case REQUEST_FOCUS:
                    LogUtils.i(tag, "isHasParent : " + isHasParent());
                    //arg1为保存的当前线程ID值
                    if (msg.arg1 >= 0 && msg.arg1 != newThreadId)
                        break;
                    LogUtils.i(tag, "REQUEST_FOCUS: " + msg.arg1);
                    if (isHasParent()) {
                        slide.showFocusView();
                        mPageController.requestFocus(KeyEvent.KEYCODE_DPAD_DOWN);
                        mGeneralPageLayout.setCategoryKey(true);
                    } else {

                        Message message2 = new Message();
                        message2.what = REQUEST_FOCUS;
                        message2.arg1 = msg.arg1;
                        mHandler.sendMessageDelayed(message2, 500);
                    }
                    break;
                case NETWORK_CONNECT:
//					getRecommendData();
                    break;
                case SHOW_LOADING:
                    if (isHasParent())
                        showLoading();
                    else
                        mHandler.sendEmptyMessageDelayed(SHOW_LOADING, 500);

                    break;
                case DISMISS_LOADING:
                    mHandler.removeMessages(SHOW_LOADING);
                    if (isHasParent()) {
                        hideLoading();
                    }
                    break;
                case SHOW_TIPS:
                    if (isHasParent())
                        showTips("数据接收错误");
                    else
                        mHandler.sendEmptyMessageDelayed(SHOW_TIPS, 500);
                    break;
                case TIMEOUT_EXIT:
                    isTimeoutExit = true;
                    hideLoading();
                    showTips("数据接收超时，请退出重试。");
                    break;
                case RETRY_GETDATA:
                    mHandler.removeMessages(SHOW_TIPS);
                    mHandler.removeMessages(SHOW_LOADING);
                    mHandler.removeMessages(TIMEOUT_EXIT);
                    retryRefresh();
                    break;
            }
        }

    };

    private boolean isHasParent() {
        if (this.getParent() == null) {
            return false;
        } else
            return true;
    }

    private void retryRefresh() {
        if (isRetry) {
            hideLoading();
            String lastName = hallName;
            hallName = null;
            refreshLayout(lastName);
            isRetry = false;
        } else {
            mHandler.sendEmptyMessageDelayed(SHOW_TIPS, 500);
        }
    }

    private void showTips(String str) {
        HallBaseActivity baseActivity = (HallBaseActivity) mContext;
        baseActivity.showTips(str);
    }

    private void hideTips() {
        HallBaseActivity baseActivity = (HallBaseActivity) mContext;
        baseActivity.hideTips();
    }

    private void showLoading() {
        HallBaseActivity baseActivity = (HallBaseActivity) mContext;
        baseActivity.showLoading();
    }

    private void hideLoading() {
        HallBaseActivity baseActivity = (HallBaseActivity) mContext;
        baseActivity.hideLoading();
    }

    public void onDestroy() {
        if (childViewList != null) {
            childViewList.clear();
            childViewList = null;
        }

        if (mPageController != null) {
            mPageController.onDestroy();
            mPageController = null;
        }

        if (mGeneralPageLayout != null) {
            mGeneralPageLayout.onDestory();
        }

        if (pageJsonMap != null) {
            pageJsonMap.clear();
            pageJsonMap = null;
        }

        if (pageTypeMap != null) {
            pageTypeMap.clear();
            pageTypeMap = null;
        }

        if (listCategoryData != null) {
            listCategoryData.clear();
            listCategoryData = null;
        }

        if (slide != null)
            slide = null;

        this.removeAllViews();
        this.destroyDrawingCache();
    }
}
